package com.pet.server.ext.action.square;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Config;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.AnswerInfo;
import com.pet.server.ext.data.Image;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.PetAdopt;
import com.pet.server.ext.data.UserInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.cache.ConstantUtil;

public class Adoptdetail extends ActionBase{

	public Integer adopt_id;
	public Page page;
	public Adoptdetail(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		adopt_id = NumberUtil.getInteger(params.getFieldValue("adopt_id"));
		if(StringUtil.isEmpty(adopt_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
		page = newPage(params);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		UserService userService = new UserService();
		JSONObject data = new JSONObject();
		PetAdopt adopt = userService.loadPetAdopt(adopt_id,user.user_id);
		if(null==adopt){
			throw ApiError.Type.EXIST_PARAM.toException();
		}
		UserInfo createUser = userService.findUserInfoById(adopt.getUser_id());
		Integer itemCount = userService.findPetItemCountByAdoptId(adopt_id);
		
		JSONObject userJson = new JSONObject();
		if(null!=createUser){
			userJson.put("nick_name", createUser.nick_name);
			userJson.put("user_id", createUser.user_id);
			userJson.put("ico_path", createUser.ico_path);
		}
		List<Image> list=userService.queryImage(adopt.adopt_id, BaseAttribute.image_type.pet_adopt.toString());
		
		data.put("user", userJson);
		
		boolean is_apply = userService.adoptIsApplay(adopt.adopt_id, user.user_id);
        data.put("is_apply", is_apply?"1":"0");		
		data.put("area", adopt.area);
		data.put("state",adopt.state);
		data.put("create_time", JsonUtil.getTime(adopt.create_time));
		data.put("breed", adopt.breed);
		data.put("breed_name", ConstantUtil.getInstance().getName(adopt.breed));
		data.put("city", adopt.city);
		data.put("city_name", adopt.city_name);
		data.put("phone", adopt.phone);
		data.put("is_birth", adopt.is_birth);
		data.put("is_injection", adopt.is_injection);
		data.put("age", adopt.age);
		data.put("item_count", itemCount);
		data.put("adopt_id", adopt.adopt_id);
		data.put("description", adopt.description);
		
		
        JSONArray imagesJson=new JSONArray();
        for(Image image:list){
        	imagesJson.add(image.src);
        }
        if(imagesJson.size()==0){
        	imagesJson.add(adopt.ico_path);
        }
        data.put("images", imagesJson);
        
        String url = Config.getValue("server.name")+"/share/adopt.do";
		data.put("share", JsonUtil.shareJson(StringUtil.dealNull(adopt.ico_path), "宠物领养", adopt.description, url));
        
		//JSONObject answerJson = new JSONObject();
		List<AnswerInfo> answerList = userService.queryAnswerInfo(adopt_id, BaseAttribute.image_type.pet_adopt.toString());
        JSONArray listJson = new JSONArray();
		for(AnswerInfo aw:answerList){
        	JSONObject awJson = new JSONObject();
        	awJson.put("user_id", aw.user_id);
        	awJson.put("nick_name", aw.nick_name);
        	awJson.put("ico_path", aw.ico_path);
        	awJson.put("create_time", JsonUtil.getTime(aw.create_time));
        	awJson.put("answer_text", aw.answer_text);
        	JSONObject replyJson = null;
        	if(aw.reply_id>0){
        		AnswerInfo reply = userService.findAnswerInfo(aw.reply_id);
        		if(null!=reply){
        			replyJson=new JSONObject();
        			awJson.put("user_id", reply.user_id);
                	awJson.put("nick_name", reply.nick_name);
                	awJson.put("ico_path", reply.ico_path);
                	awJson.put("create_time", JsonUtil.getTime(reply.create_time));
                	awJson.put("answer_text", reply.answer_text);
        		}
        	}
        	awJson.put("replay", replyJson);
        	listJson.add(awJson);
        }
		data.put("list", listJson);
		//data.put("answer", answerJson);
		/**
		adopt.is_apply=BaseAttribute.LOGIC_TRUE.equals(adopt.is_apply)?BaseAttribute.LOGIC_TRUE:BaseAttribute.LOGIC_FALSE;
		adopt.list= userService.queryImage(adopt.adopt_id, BaseAttribute.image_type.pet_adopt.toString());
		JSONObject o = new JSONObject();
		List<AnswerInfo> comment = userService.queryAnswerInfo(adopt_id, BaseAttribute.image_type.pet_adopt.toString());
        JSONObject adt = BeanToJson.beanToJson(adopt);
        result.setPage(adt, page);
		*/
		result.put("data", data);
	    return result;
	}


}
